//
//  TransferTaskTableViewCell.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/20.
//

#import "TransferTaskTableViewCell.h"

#import "userPhoneView.h"
#import "YTBaseSelectStringView.h"
#import "TemplateFoundDetailsModel.h"

@interface TransferTaskTableViewCell ()

///任务指派
@property (nonatomic ,strong) UIView * appointorView;
@property (nonatomic ,strong) userPhoneView * phone1;
@property (nonatomic ,strong) userPhoneView * phone2;
@property (nonatomic ,strong) userPhoneView * phone3;
@property (nonatomic ,strong) userPhoneView * phone4;
@property (nonatomic ,strong) userPhoneView * phone5;
@property (nonatomic ,strong) userPhoneView * phone6;
@property (nonatomic ,strong) userPhoneView * phone7;
@property (nonatomic ,strong) userPhoneView * phone8;

@property (nonatomic ,strong) NSArray< userPhoneView * > *phoneArray;

@property (nonatomic ,strong) YTBaseSelectStringView * ZJview;

@end
@implementation TransferTaskTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.whiteColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    
    
    UIView * appointorView = [UIView new];
    self.appointorView = appointorView;
    appointorView.backgroundColor = KcolorHex(@"#F6F7F9");
    [DTKTool YJ:kwt(5) andView:appointorView];
    [self.contentView addSubview:appointorView];
    [appointorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));
        make.height.offset(kwt(225));
    }];
    
    
    UILabel * appointorlab = [DTKTool labelWithText:@"*任务指派人" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
    appointorlab.attributedText = [DTKTool AttributedStringWithStr:@"*选择转交对象" andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kCFont(17) andPattenr:@"[*]"];
    [appointorView addSubview:appointorlab];
    [appointorlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.offset(kwt(15));
    }];
    
    userPhoneView * phone1 = [self phoneviewWithIndex:0];
    self.phone1 = phone1;
    [appointorView addSubview:phone1];
    [phone1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.offset(kwt(45));
        make.left.offset(kwt(10));
    }];
    
    userPhoneView * phone2 = [self phoneviewWithIndex:1];
    self.phone2 = phone2;
    [appointorView addSubview:phone2];
    [phone2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.equalTo(phone1);
        make.left.equalTo(phone1.mas_right).offset(kwt(7));
    }];
    
    
    userPhoneView * phone3 = [self phoneviewWithIndex:2];
    self.phone3 = phone3;
    [appointorView addSubview:phone3];
    [phone3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.equalTo(phone2);
        make.left.equalTo(phone2.mas_right).offset(kwt(7));
    }];
    
    
    userPhoneView * phone4 = [self phoneviewWithIndex:3];
    self.phone4 = phone4;
    [appointorView addSubview:phone4];
    [phone4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.equalTo(phone3);
        make.left.equalTo(phone3.mas_right).offset(kwt(7));
    }];
    
    userPhoneView * phone5 = [self phoneviewWithIndex:4];
    self.phone5 = phone5;
    [appointorView addSubview:phone5];
    [phone5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.left.equalTo(phone1);
        make.top.equalTo(phone1.mas_bottom).offset(kwt(12));
    }];
    
    userPhoneView * phone6 = [self phoneviewWithIndex:5];
    self.phone6 = phone6;
    [appointorView addSubview:phone6];
    [phone6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.equalTo(phone5);
        make.left.equalTo(phone5.mas_right).offset(kwt(7));
    }];
    
    userPhoneView * phone7 = [self phoneviewWithIndex:6];
    self.phone7 = phone7;
    [appointorView addSubview:phone7];
    [phone7 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.equalTo(phone6);
        make.left.equalTo(phone6.mas_right).offset(kwt(7));
    }];
    
    userPhoneView * phone8 = [self phoneviewWithIndex:7];
    self.phone8 = phone8;
    [appointorView addSubview:phone8];
    [phone8 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.equalTo(phone7);
        make.left.equalTo(phone7.mas_right).offset(kwt(7));
    }];
    
    self.phoneArray = @[self.phone1,self.phone2,self.phone3,self.phone4,self.phone5,self.phone6,self.phone7,self.phone8];
    
    UIView * timebackview = [UIView new];
    [self.contentView addSubview:timebackview];
    [timebackview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(appointorView.mas_bottom).offset(kwt(5));
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.height.offset(kwt(65));
    }];
    YTBaseSelectStringView * ZJview = [YTBaseSelectStringView NewView];
    self.ZJview = ZJview;
    ZJview.name = @"转交原因";
    ZJview.forceFlag = YES;
    MJWeakSelf
    ZJview.block = ^(NSString * _Nonnull content) {
        if(weakSelf.openYYBlock){
            weakSelf.openYYBlock(weakSelf.model);
        }
    };
    [timebackview addSubview:ZJview];
    [ZJview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
}

- (userPhoneView *)phoneviewWithIndex:(int)index{
    userPhoneView * phoneview = [userPhoneView NewView];
    [phoneview setbackImg:[UIImage imageNamed:@"transferred_person_image"]];

    MJWeakSelf
    phoneview.deleteBlock = ^{
        [weakSelf.model.tempUserModel removeObjectAtIndex:index];
        if(weakSelf.reLoadBlock){
            weakSelf.reLoadBlock();
        }
    };
    phoneview.openImgBlock = ^{
        if(weakSelf.openUserBlock){
            weakSelf.openUserBlock(weakSelf.model);
        }
    };
    phoneview.addImgBlock = ^{
        if(weakSelf.openUserBlock){
            weakSelf.openUserBlock(weakSelf.model);
        }
    };
    return phoneview;
}

- (void)setModel:(CancelAndTransferModel *)model{
    _model = model;
    self.ZJview.content = model.applyReason;
    for (int i = 0; i < 8; i++) {
        if(model.tempUserModel.count > i){
            self.phoneArray[i].hidden = NO;
            if(model.tempUserModel[i].userIdentity == 1){///管理员
                [self.phoneArray[i] setImg:[UIImage imageNamed:@"task_assigner_defaul_avatar"]];
            } else {
                [self.phoneArray[i] setImg:[UIImage imageNamed:@"task_assigner_defaul_staff"]];
            }
            [self.phoneArray[i] setContent:model.tempUserModel[i].realName];
        } else  if(model.tempUserModel.count == i){
            self.phoneArray[i].hidden = NO;
            [self.phoneArray[i] deleteImg];
            if(i > 0){//只能转交1人
                self.phoneArray[i].hidden = YES;

            }
        } else {
            self.phoneArray[i].hidden = YES;
        }
    }
    [self.appointorView mas_updateConstraints:^(MASConstraintMaker *make) {
        if(model.tempUserModel.count > 3){
            make.height.offset(kwt(225));
        } else {
            make.height.offset(kwt(135));
        }
    }];
}
@end
